package java学习.第18章_JDK8_17新特性;

import org.junit.Test;

/**
 * ClassName: JDK17_switch加instanceof交替
 * Package: java学习.第18章_JDK8_17新特性
 * Description:
 *
 * @Author 张蛟龙
 * @Create 2023/4/29 21:36
 * @Version 1.0
 */
public class JDK17_switch加instanceof交替 {
    @Test
    public void test(){
        instanceof单独("aweidhj");
        System.out.println(switch加instanceof("aweidhj"));
        System.out.println(switch加instanceof(1634.6));
        //高级用法  见User
        User u= new User(1,"名字",55);
        System.out.println(u.equals(null));
    }

    private static void instanceof单独(Object o){
        //如果是  自动强转
        if(o instanceof String s){
            System.out.println("是字符串长度："+s.length());
        }else if(o instanceof User user){
            System.out.println(user.toString());
        }
    }
    private static String switch加instanceof(Object o){
        String str = switch (o){
            case String s:
                yield  s + "我是 String 类型";
            case Integer i:
                yield  i + "我是 Integer 类型";
            case Long l:
                yield  l + "我是 Long 类型";
            case Double d:
                yield  d + "我是 Double 类型";
            default:
                yield "暂且不知";
        };
        return str;
    }
}
